package com.firefighting.maintenancer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class GetTaskMaintenanceContent
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/GetTaskMaintenanceContent" })
public class GetTaskMaintenanceContent extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		
		String userName=request.getParameter("userName");
		String task_id=request.getParameter("task_id");
		String second_project_id=request.getParameter("second_project_id");
		//String maintenanceplan_id=request.getParameter("maintenanceplan_id");
		
		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();	
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		JSONArray maintenance_content_ja=new JSONArray();
		try {
			connection = JdbcUtils.getConnection();
			String sql = "select mtc.id,mtc.serial,mtc.test_name,mtc.test_content,mc.state_finished "
					+ "from maintenance_test_content mtc,task t,task_item d,users u,maintenanceplan_content mc "
					+ "where t.id=? and u.userName=? and u.userId=t.app_id_account and t.id=d.task_id and d.maintenance_detail_id=mtc.Id and mtc.Father_maintenance_id=? "
					+ "and mc.maintenanceplan_id=t.maintenanceplan_id and mc.maintenance_content_id=d.maintenance_detail_id and t.building_id=mc.building_id";
			ps = connection.prepareStatement(sql);
			ps.setString(1, task_id);
			ps.setString(2, userName);
			ps.setString(3, second_project_id);
			rs=ps.executeQuery();
			MYJSONObject maintenance_content_jb=new MYJSONObject();
			while(rs.next())
		    {	
				maintenance_content_jb.put("maintenance_test_content_id", rs.getString(1));
				maintenance_content_jb.put("maintenance_test_content_serial", rs.getString(2));
				maintenance_content_jb.put("maintenance_test_content_name", rs.getString(3));
				maintenance_content_jb.put("maintenance_test_content_all", rs.getString(4));
				maintenance_content_jb.put("maintenance_test_content_state_finished", rs.getString(5));
				maintenance_content_ja.add(maintenance_content_jb.toString());
	        }	
			result_jb.put("status", "true");
			result_jb.put("task_id", task_id);
			result_jb.put("message", maintenance_content_ja);
	}
	catch (SQLException e) {
		e.printStackTrace();
		result_jb.put("status", "false");
		result_jb.put("message", "服务器异常");
	} finally {
		
		out.print(result_jb.toString());
		out.flush();
		out.close();
	}

	}
}
